package com.wzs;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.Properties;

public class TestJDBC {

    public static void main(String[] args) throws ClassNotFoundException, SQLException {

        //0.读取配置文件
        InputStream is = TestJDBC.class.getClassLoader().getResourceAsStream("jdbc.properties");//类加载器

        Properties pro = new Properties();
        try {
            pro.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String className = pro.getProperty("driver");
        String url =pro.getProperty("url");
        String username =pro.getProperty("user");
        String password =pro.getProperty("password");

        //1.加载驱动
        Class.forName(className);

        //2.获取连接
        Connection conn = DriverManager.getConnection(url, username,password);

        //3.执行sql
        String sql = "select * from user";
        PreparedStatement pstmt = conn.prepareStatement(sql);

        //4.获取结果
        ResultSet rs = pstmt.executeQuery();

        //5.读取数据
        ArrayList<User> users = new ArrayList<>();
        while (rs.next()){
            int id = rs.getInt("id");
            String name = rs.getString("name");
            User u = new User(id,name);
            users.add(u);
        }

        //6.集合遍历出结果
        for (User user : users) {
            System.out.println(user);
        }

    }
}











